package by.grsu.shakh.page.librarian;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.shakh.app.LibraryWebApplication;
import by.grsu.shakh.dao.CatalogDao;
import by.grsu.shakh.entity.Catalog;

/**
 * "edit" page for Catalog object.
 * 
 * @author shakh_andrew
 */
public class CatalogEditPage extends WebPage {

	public CatalogEditPage(final Catalog catalog) {
		super();
		final Form<Catalog> form = new Form<Catalog>("form",
				new CompoundPropertyModel<Catalog>(catalog));
		add(form);

		form.add(new TextField<String>("name"));

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				final CatalogDao catalogDao = new CatalogDao(
						LibraryWebApplication.DATA_XML_FOLDER);

				if (catalog.getId() == null) {

					catalogDao.saveNew(catalog);
				} else {

					catalogDao.update(catalog);
				}

				setResponsePage(new ListCatalogPage());
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new ListCatalogPage());
			}
		});
	}
}
